package com.ruoyi.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.domain.GoodsPurchaseDetail;
import com.ruoyi.mapper.GoodsPurchaseDetailMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mapper.GoodsPurchaseMapper;
import com.ruoyi.domain.GoodsPurchase;
import com.ruoyi.service.IGoodsPurchaseService;

/**
 * 采购订单Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-05-13
 */
@Service
public class GoodsPurchaseServiceImpl implements IGoodsPurchaseService 
{
    @Autowired
    private GoodsPurchaseMapper goodsPurchaseMapper;

    @Autowired
    private GoodsPurchaseDetailMapper goodsPurchaseDetailMapper;

    /**
     * 查询采购订单
     * 
     * @param purchaseId 采购订单主键
     * @return 采购订单
     */
    @Override
    public GoodsPurchase selectGoodsPurchaseByPurchaseId(Long purchaseId)
    {
        return goodsPurchaseMapper.selectGoodsPurchaseByPurchaseId(purchaseId);
    }

    /**
     * 查询采购订单列表
     * 
     * @param goodsPurchase 采购订单
     * @return 采购订单
     */
    @Override
    public List<GoodsPurchase> selectGoodsPurchaseList(GoodsPurchase goodsPurchase)
    {
        return goodsPurchaseMapper.selectGoodsPurchaseList(goodsPurchase);
    }

    ///**
    // * 新增采购订单
    // *
    // * @param goodsPurchase 采购订单
    // * @return 结果
    // */
    //@Override
    //public int insertGoodsPurchase(GoodsPurchase goodsPurchase)
    //{
    //    return goodsPurchaseMapper.insertGoodsPurchase(goodsPurchase);
    //}

    /**
     * 新增采购订单
     *
     * @param goodsPurchase 采购订单
     * @return 结果
     */
    @Override
    public int insertGoodsPurchaseMany(GoodsPurchase goodsPurchase)
    {
        int insert = goodsPurchaseMapper.insertGoodsPurchase(goodsPurchase);
        if (insert>0) {
            List<GoodsPurchaseDetail> children = goodsPurchase.getChildren();
            children.stream().peek(detail->{
                detail.setPurchaseId(goodsPurchase.getPurchaseId());
            }).collect(Collectors.toList());


            System.out.println("huihuhuhuhhu");
            System.out.println(children);
            goodsPurchaseDetailMapper.insertMany(children);

        }

        return insert;
    }

    /**
     * 修改采购订单
     * 
     * @param goodsPurchase 采购订单
     * @return 结果
     */
    @Override
    public int updateGoodsPurchase(GoodsPurchase goodsPurchase)
    {
        return goodsPurchaseMapper.updateGoodsPurchase(goodsPurchase);
    }

    /**
     * 批量删除采购订单
     * 
     * @param purchaseIds 需要删除的采购订单主键
     * @return 结果
     */
    @Override
    public int deleteGoodsPurchaseByPurchaseIds(Long[] purchaseIds)
    {
        return goodsPurchaseMapper.deleteGoodsPurchaseByPurchaseIds(purchaseIds);
    }

    /**
     * 删除采购订单信息
     * 
     * @param purchaseId 采购订单主键
     * @return 结果
     */
    @Override
    public int deleteGoodsPurchaseByPurchaseId(Long purchaseId)
    {
        return goodsPurchaseMapper.deleteGoodsPurchaseByPurchaseId(purchaseId);
    }

    @Override
    public List<GoodsPurchase> selectGoodsPurchaseListAll() {
        return goodsPurchaseMapper.selectListAll();
    }

    @Override
    public int cheakSn(String purchaseSn) {
        return goodsPurchaseMapper.selectCountByPurchaseSn(purchaseSn);
    }
}
